package com.zyn.rsgl.controller;


import com.zyn.rsgl.pojo.Salary;
import com.zyn.rsgl.service.SalaryService;
import com.zyn.rsgl.utils.commonResult.R;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * <p>
 * 薪资表 前端控制器
 * </p>
 *
 * @author zcs
 * @since 2025-02-26
 */
@RestController
@RequestMapping("/rsgl/salary")
public class SalaryController {
    @Resource
    private SalaryService salaryService;

    // 分页查询
    @GetMapping("/list")
    public R list(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize,
            @RequestParam(required = false) String userId,
            @RequestParam(required = false) String positionName) {
        return salaryService.listPage(pageNum, pageSize, userId, positionName);
    }

    // 根据ID查询
    @GetMapping("/get/{salaryId}")
    public R get(@PathVariable("salaryId") String salaryId) {
        Salary byId = salaryService.getById(salaryId);
        if (byId != null) {
            return R.ok(byId);
        }
        return R.fail("查询失败");
    }

    // 根据userID查询
    @GetMapping("/getUser/{userId}")
    public R getByUser(@PathVariable("userId") String userId) {
        Salary byId = salaryService.getByUserId(userId);
        if (byId != null) {
            return R.ok(byId);
        }
        return R.fail("查询失败");
    }

    // 新增
    @PostMapping("/add")
    public R add(@RequestBody Salary salary) {
        // 判断是否存在
        if (salaryService.getById(salary.getId()) != null) {
            return R.fail("用户已存在");
        }
        return salaryService.save(salary) ? R.ok() : R.fail("新增失败");
    }

    // 修改
    @PutMapping("/update")
    public R update(@RequestBody Salary salary) {
        return salaryService.updateById(salary) ? R.ok() : R.fail("修改失败");
    }

    // 删除
    @DeleteMapping("/delete/{salaryId}")
    public R delete(@PathVariable("salaryId") String salaryId) {
        return salaryService.removeById(salaryId) ? R.ok() : R.fail("删除失败");
    }
}
